package com.jerkin.init;


import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @version V1.0
 * @ClassName: InitInterfaceInfo
 * @Description:
 * @author: huhongzhi
 * @date: 2021/4/22 23:09
 */
public class InitInterfaceInfo {

    private static final Map<String, Class<?>> CLAZZ_MAP = new ConcurrentHashMap<String, Class<?>>(16);


    public static void putInterfaceInfo(String clazzName, Class<?> clazz) {
        CLAZZ_MAP.put(clazzName, clazz);
    }

    public static Class<?> getInterfaceInfo(String clazzName) {
        return CLAZZ_MAP.get(clazzName);
    }

    public static Object instance(String clazzName) {
        Class<?> clazz = CLAZZ_MAP.get(clazzName);
        return Optional.ofNullable(clazz).map(e -> {
            try {
                return e.newInstance();
            } catch (InstantiationException instantiationException) {
                System.out.println("实列化失败," + instantiationException);
            } catch (IllegalAccessException illegalAccessException) {
                System.out.println("实列化失败," + illegalAccessException);
            }
            return null;
        }).orElse(null);
    }
}
